/* eslint-disable */
let domain = location.host.substr(location.host.indexOf('.'));
let expires = 7;

function Trim(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
}
let Cookie = {
    //获取cookie
    get: function(key = null) {
        let cookieArr = [];
        let cookie = {};
        let arr;
        cookieArr = document.cookie.split(';');
        cookieArr.forEach(function(value, index, arr) {
            arr = value.split('=');
            if (!cookie[Trim(arr[0])]) {
              cookie[Trim(arr[0])] = arr[1];
            }
        })
        return cookie[key] ? cookie[key] : null;
    },
    //设置cookie
    set: function(key, value) {
        var exp = new Date();
        exp.setTime(exp.getTime() + expires * 24 * 60 * 60 * 1000);
        document.cookie = key + "=" + escape(value) + ";expires=" + exp.toGMTString() + ';path=/';
    },
    //删除单个cookie
    delete: function(key) {
        document.cookie = key + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
        document.cookie = key + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=" + domain;
    },
    //删除所有cookie
    clean: function() {
        var cookies = document.cookie.split(";");
        for (var i = 0; i < cookies.length; i++) {
            var cookie = cookies[i];
            var eqPos = cookie.indexOf("=");
            var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
            document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
        }
        if (cookies.length > 0) {
            for (var i = 0; i < cookies.length; i++) {
                var cookie = cookies[i];
                var eqPos = cookie.indexOf("=");
                var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
                document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=" + domain;
            }
        }
    }
}
export default Cookie;
